package com.apes.scm.masterdata.party.model;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 功能：接入商
 *
 * @author xul
 * @create 2018-01-23 14:35
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = AccessProvider.CONST_TABLE_NAME)
public class AccessProvider extends Model {
    public static final String CONST_TABLE_NAME = "mas_access_provider";

    @Id
    @Column(name = "id")
    @Comment("接入商编码")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "接入商")
    private String id; //接入商ID

    @Name
    @Column(name = "name", nullable = false, length = 80)
    @Comment("接入商名称")
    private String name; //接入商名称

    @OneToMany(fetch=FetchType.LAZY)
    @JoinTable(name="s_access_company",
            joinColumns={@JoinColumn(name="access_id",referencedColumnName = "id")},
            inverseJoinColumns={@JoinColumn(name="company_id",referencedColumnName = "id")},
            indexes = {@Index(name = "idx_access_company_id", columnList = "access_id,company_id", unique = true)})
    private List<PartyRole> companys = new ArrayList<>();// 拥有的公司

    // 定义接入商前端界面转化
    @Transient
    private List<JSONObject> companyLits;
//
//
//    @OneToMany(mappedBy = "accessProvider", cascade = CascadeType.ALL)
//    private List<PartyRole> companys = new ArrayList(); //当事人角色
//
//    public List<PartyRole> getCompanys() {
//        if (companys == null) return null;
//        for (PartyRole partyRole : companys) {
//            partyRole.setAccessProvider(this);
//        }
//        return companys;
//    }


    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    @Column(name = "actual_controller", length = 250)
    @Comment("实际控制人")
    private String actualController; //实际控制人

    /*
     * 操作界面转换Map
     * */
    @Transient
    private JSONObject corporates;  // 所属
}
